/***********************************************************************
 * <copyright file="Global.asax.cs" company="VN Solution">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@vnsol.net or sonhb@vnsol.vn
 * Website:
 * Create Date: Tuesday, September 30, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using MVC;
using Newtonsoft.Json;
using VNSSystem;
using VNSSystem.Controllers.Security;
using VNSSystem.Models;

namespace VNSWebAdmin
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    /// <summary>
    /// 
    /// </summary>
    public class MvcApplication : HttpApplication
    {
        /// <summary>
        /// Application_s the start.
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
            
            ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();

            DevExpress.Web.ASPxClasses.ASPxWebControl.CallbackError += Application_Error;
        }

        /// <summary>
        /// Handles the PostAuthenticateRequest event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        //protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        //{
        //    var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        //    if (authCookie == null) return;
        //    var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        //    if (authTicket == null) return;
        //    var serializeModel = JsonConvert.DeserializeObject<CustomPrincipalSerializeModel>(authTicket.UserData);
        //    var newUser = new CustomPrincipal(authTicket.Name)
        //                  {
        //                      UserId = serializeModel.UserId,
        //                      FirstName = serializeModel.FirstName,
        //                      LastName = serializeModel.LastName,
        //                      Roles = serializeModel.Roles
        //                  };
        //    HttpContext.Current.User = newUser;
        //}

        /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Application_Error(object sender, EventArgs e)
        {
            HttpContext.Current.Server.GetLastError();            
        }
    }
}